/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: ticketOperate.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月18日 星期四 01时15分09秒
***************************************************/

#include "ticketOperate.h"


//管理员操作系统
void ticketOperateSystem(void){
	//会员信息变量
	TIC *head_ticket = NULL;
	FILE *fp_ticket = NULL;

	int model = 0;

	while(1){
		printf("---管理员票务系统---\n");
		printf("1--建立票务信息（选择后原有数据清除）\n");
		printf("2--读取票务信息\n");
		printf("3--添加新票务\n");
		printf("4--查询票务\n");
		printf("5--删除票务\n");
		printf("6--修改票务信息\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			break;
		}

		switch(model){
			case 1:
				createManagerToTicket(fp_ticket,head_ticket);
				break;
			case 2:
				outputManagerToTicket(fp_ticket,head_ticket);
				break;
			case 3:
				fp_ticket = addTicket(fp_ticket,head_ticket);
				break;
			case 4:
				queryManagerToTicket(fp_ticket,head_ticket);
				break;
			case 5:
				fp_ticket = delTicket(fp_ticket,head_ticket);
				break;
			case 6:
				fp_ticket = amendTicket(fp_ticket,head_ticket);
				break;
			default:
				printf("输入错误！\n");
				break;
		}
	}
}

//管理员建立票务信息函数
void createManagerToTicket(FILE *fp,TIC *head){
	head = createTicketHead();
	head = createTicketLink(head);
	outputTicketLink(head);

	fp = fopen("./Ticket.txt","w");
	if(fp == NULL){
		perror("fopen Ticket!");
		exit(-1);
	}

	fp = ticketWriteToFile(fp,head);

	fclose(fp);

	freeTicketLink(head);
	printf("票务信息保存成功！\n");
}

//管理员读取会员信息函数
void outputManagerToTicket(FILE *fp,TIC *head){
	fp = fopen("./Ticket.txt","r");
	if(fp == NULL){
		perror("fopen Ticket!");
		exit(-1);
	}

	head = createTicketHead();
	head = ticketReadFile(fp,head);
	printf("票务信息：\n");
	outputTicketLink(head);
	fclose(fp);

	freeTicketLink(head);
	printf("票务信息读取成功！\n");
}

//会员添加函数
FILE *addTicket(FILE *fp,TIC *head){
	fp = fopen("./Ticket.txt","a+");
	if(fp == NULL){
		perror("fopen Ticket!");
		exit(-1);
	}
	head = createTicketHead();
	head = createTicketLink(head);
	outputTicketLink(head);
	
	char ch[5] = {0};
	printf("请确认：yes or no?");
	scanf("%s",ch);

	if(strcmp(ch,"yes") == 0){
		fp =  ticketWriteToFile(fp,head);
		rewind(fp);
		freeTicketLink(head);

		head = createTicketHead();
		head = ticketReadFile(fp,head);
		outputTicketLink(head);
		freeTicketLink(head);
		fclose(fp);
		printf("添加成功！\n");
	}else{
		fclose(fp);
		freeTicketLink(head);
		printf("添加失败！\n");
	}
	return fp;
}

//管理员查询函数 查询成功返回id，取消返回0，未找到返回1
int queryManagerToTicket(FILE *fp,TIC *head){
	int model = 0;
	int n = 0;

	while(1){
		printf("---票务信息查询系统---\n");
		printf("1--输入id查询\n");
		printf("2--输入起点查询\n");
		printf("3--输入终点查询\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			break;
		}

		n = 0;
		switch(model){
			case 1:
				printf("请输入需要查询的id\n");
				printf("id:");
				scanf("%d",&n);

				if(n >= 99999 && n <= 1000000){
					n = queryIdToTicket(fp,head,n);
				}else{
					printf("id输入错误！\n");
				}
				break;
			case 2:
				n = queryOriginToTicket(fp,head);
				break;
			case 3:
				n = queryFinishToTicket(fp,head);
				break;
			default:
				printf("输入错误！\n");
				break;
		}
	}
	return n;
}

//票务删除函数
FILE *delTicket(FILE *fp,TIC *head){
	int n = 0;
	n = queryManagerToTicket(fp,head);
	if(n == 1){
		printf("需要删除的票务不存在！\n");
	}else{
		head = createTicketHead();
		fp = fopen("./Ticket.txt","r");
		if(fp == NULL){
			perror("fopen Ticket!");
			exit(-1);
		}
		head = ticketReadFile(fp,head);
		fclose(fp);

		head = delTicketLink(head,n);
		outputTicketLink(head);

		fp = fopen("./Ticket.txt","w");
		if(fp == NULL){
			perror("fopen Ticket!");
			exit(-1);
		}
		fp = ticketWriteToFile(fp,head);
		freeTicketLink(head);
		fclose(fp);

		printf("票务删除成功！\n");
	}

	return fp;
}

//票务信息修改函数
FILE *amendTicket(FILE *fp,TIC *head){
	int n = 0;
	n = queryManagerToTicket(fp,head);
	if(n == 1){
		printf("需要修改的票务不存在！\n");
	}else{
		head = createTicketHead();
		fp = fopen("./Ticket.txt","r");
		if(fp == NULL){
			perror("fopen Ticket!");
			exit(-1);
		}
		head = ticketReadFile(fp,head);
		fclose(fp);

		head = amendTicketLink(head,n);
		outputTicketLink(head);

		fp = fopen("./Ticket.txt","w");
		if(fp == NULL){
			perror("fopen Ticket!");
			exit(-1);
		}
		fp = ticketWriteToFile(fp,head);
		freeTicketLink(head);
		fclose(fp);

		printf("票务信息修改成功！\n");
	}

	return fp;
}
